﻿using System;
using SimpleServiceBus.Messages.Management.Monitoring;

namespace SimpleServiceBus.Endpoint.Management.Monitoring
{
    public static class EndpointMeasurementExtensions
    {
        public static void AddMeasurement(this EndpointMeasurementSummary summary,EndpointMeasurement measurement)
        {
            summary.LastMeasurement = measurement;
            summary.SampleCount++;


            if (measurement.DataType == MeasurementDataType.Numeric)
            {
                double doubleVal;
                Double.TryParse(measurement.Value, out doubleVal);

                summary.Sum += doubleVal;
                summary.Average = summary.Sum / summary.SampleCount;

                if
                    (doubleVal > summary.Max) summary.Max = doubleVal;
                else if
                    (doubleVal < summary.Min) summary.Min = doubleVal;
            }
        }
    }
}